package com.foreveross.proxyip.listen;

import javax.servlet.ServletContextEvent;

import org.apache.log4j.Logger;

import com.dayatang.spring.webapp.DayatangContextLoaderListener;
import com.foreveross.proxyip.application.ISystemInitApplication;
import com.foreveross.proxyip.infra.conf.proxyip.ProxyIpPropertiesLoader;

public class AppStartupListener extends DayatangContextLoaderListener {

	private static final Logger log = Logger
			.getLogger(AppStartupListener.class);

	public void contextDestroyed(ServletContextEvent arg0) {
		log.info("destroy config file......");
		com.foreveross.proxyip.infra.conf.ConfigContainer.getInstance().destroy();
//		com.foreveross.proxyip.infra.InstanceFactory.getInstance(ISystemInitApplication.class).closePoolValidate();
		super.contextDestroyed(arg0);
	}

	public void contextInitialized(ServletContextEvent arg0) {
		
		//一个spring上下文初始化
		com.foreveross.proxyip.infra.InstanceFactory.setServletContext(arg0.getServletContext());
		// 加载配置文件
		log.info("init config file......");
		// 系统默认的config.properties配置文件
		com.foreveross.proxyip.infra.conf.ConfigContainer.getInstance().register(com.foreveross.proxyip.infra.conf.system.DefaultPropertiesLoader.class);
		// hbase数据库配置文件(可以改为像jdbc.properties文件一样由spring加载)
		com.foreveross.proxyip.infra.conf.ConfigContainer.getInstance().register(com.foreveross.proxyip.infra.db.hbase.HBasePropertiesLoader.class);
		//代理IP使用到的配置信息
		com.foreveross.proxyip.infra.conf.ConfigContainer.getInstance().register(ProxyIpPropertiesLoader.class);
		
		//必须先加载这个，才能加载后面的初始化代理ＩＰ池
		log.info("init dayatang/spring context......");
		super.contextInitialized(arg0);
		
		//初始化代理IP池
		log.info("init proxyip pool data......");
		// markfb
		com.foreveross.proxyip.infra.InstanceFactory.getInstance(ISystemInitApplication.class).init();
	}

}
